package com.imddy.tweb.controller;


import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Controller
@RequestMapping("/upload")
public class UploadController {

	private static Log log = LogFactory.getLog(UploadController.class);

	@RequestMapping(value="file", method=RequestMethod.POST)
	public String file() {
		log.info("uploadController... file()...");

		
		return "/upfileresult";
	}
	
	@RequestMapping(value="file1", method=RequestMethod.POST)
	public String upload1(HttpServletRequest request, HttpServletResponse response) {
		log.info("uploadController... file1()...");
		String path = request.getSession().getServletContext().getRealPath("upload");
		MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> map = multipartHttpServletRequest.getFileMap();
		log.info("path:" + path);
		File file = new File(path);
		if (!file.exists()) {
			file.mkdirs();
		}
		try {
			for (Map.Entry<String, MultipartFile> entity : map.entrySet()) {
				MultipartFile multipartFile = entity.getValue();
				File ff = new File(path, multipartFile.getOriginalFilename());
				multipartFile.transferTo(ff);
			}
			return "/success";
		} catch (Exception e) {
			e.printStackTrace();
			return "/error";
		}
	}
	
	
	@RequestMapping(value="file0",method=RequestMethod.POST)
	public String upload0(@RequestParam MultipartFile ttupfile,HttpServletRequest request, HttpServletResponse response) throws IOException {
		log.info("uploadController... file0()...");
		String path = request.getSession().getServletContext().getRealPath("upload");
		File file0 = new File(path, ttupfile.getOriginalFilename());
		ttupfile.transferTo(file0); // 保存文件
		return "/success";
	}

	
}
